<template>
	<view class="wrap">
		<view class="tips">
			<image class="b-image" src="/static/imgs/loading_image.png" alt="" mode="aspectFit"></image>
			<view class="b-tit">尝试自动登录，请稍后....</view>
		</view>
	</view>
</template>

<script>
	import {
		wechatAutoLogin
	} from '@/service/api/user.js'
	import {
		smartLogin
	} from '@/service/request/main.js'
	import {
		encodeRedirectUrl,
		decodeRedirectUrl
	} from '@/common/util.js'
	export default {
		data() {
			return {}
		},
		onLoad(options) {
			console.log('mini_login onLoad', options)
			// 微信小程序自动登录
			const returnPage = options.from ? decodeRedirectUrl(options.from) : '/pages/home/index';
			uni.login().then(([err, res]) => {
				console.log('uni.login', err, res);
				if (!err) {
					wechatAutoLogin(res.code).then(([err, res]) => {
						console.log('wechatAutoLogin', err, res)
						if (!err) {
							console.error('login success', 'mini_login')
							this.$store.commit('setAccessToken', res.token)
							// 返回之前页面
							uni.reLaunch({
								url: returnPage
							})
						} else {
							console.error('login fail', 'mini_login')
							smartLogin(returnPage)
						}
					})
				} else {
					console.error('code fail', 'mini_login')
					smartLogin(returnPage)
				}
			})
		},
		methods: {

		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: #ffffff;
		height: 100%;
	}

	.wrap {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100%;
	}

	.tips {
		display: flex;
		flex-direction: column;
		align-items: center;

		.b-image {
			width: 300upx;
			height: 240upx;
		}

		.b-tit {
			margin-top: 40upx;
			color: #666;
			font-size: 28upx;
		}
	}
</style>
